package com.gaoyuzhe.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author gaoyuzhe
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @HystrixCommand
    @Override
    public String paymentOk(Integer id) {
        return Thread.currentThread().getName()+",执行成功了,ID为  "+id+"   nice!";
    }

    @HystrixCommand(fallbackMethod = "paymentTimeoutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    @Override
    public String paymentTimeout(Integer id) {
        int sleepTime = 5;
        try {
            TimeUnit.SECONDS.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Thread.currentThread().getName()+"执行成功了,ID为  "+id+"我休息了(秒)" + sleepTime +"   nice!";
    }

    public String paymentTimeoutHandler(Integer id){
        return Thread.currentThread().getName()+",paymentTimeoutHandler, 我给你处理了";
    }

}
